//index.js
import api from '../../../utils/api.js'
//获取应用实例
var app = getApp();

Page({
  data: {
    page:1,
    pid:'',
    imageurl:api.imageurl,
    banner: [],//分类banner
    classify: [],//二级菜谱分类
    cookbooks: [] //分类全部菜品
  },
  onLoad: function (option) {
    console.log(option)
    var that = this;
    var pid = option.classifyId
    var name = option.name
    wx.setNavigationBarTitle({
      title: name + "专区",
    })
    that.setData({
      pid: pid,
      name:name
    })
    that.getCookbookList();
    api.getClassifyBanner({
      data: {
        pid: that.data.pid
      },
      success: function (res) {
        console.log(res)
        var data = res.data.result;
        console.log("data",data)
        var banner = data.banner;
        var classify = data.classify;
        that.setData({
          banner: banner,
          classify: classify,
        })
      }
    })
  },
  getCookbookList: function (){
    var that = this
    api.getCookbookList({
      data:{
        page:that.data.page,
        pid: that.data.pid
      },
      success: function (res) {
        var cookbooks = res.data.result;
        var cookbookss = that.data.cookbooks
        for (var i in cookbooks){
          cookbookss.push(cookbooks[i])
        }
        that.setData({
          cookbooks: cookbookss,
          page: that.data.page + 1
        })
      }
    })
  },
  //点击banner跳转链接，若无id,则不跳转
  clickBanner: function (e) {
    var cookId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?cookId=' + cookId
    });
  },

// 进入分类菜谱列表页
  showCookList:function(e){
    var classifyId = e.currentTarget.dataset.classifyId;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "../list/list?classifyId=" + classifyId + "&name=" + name
    });
  },
  // 查看详情
  showDetail: function (e) {
    var cookId = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var praiseCount = e.currentTarget.dataset.praise;
    wx.navigateTo({
      url: '../detail/detail?cookId=' + cookId + "&name=" + name + "&praise=" + praiseCount
    })
  },

  onReachBottom: function () {
    var that = this;
    that.getCookbookList();
  },
  /**
* 回首页
*/
  gohome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
     * 分享
     */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: "小巴依",
      path: '/pages/cookbook/index/index?classifyId=' + that.data.pid + '&name=' + that.data.name
    }
  },
})
